package concurrent.c_001_Synchronized;

import lombok.SneakyThrows;

import java.util.Random;

/**
 * 分析一下这个程序的输出
 * 加或不加 volatile 与 synchronized 的情况
 */
public class T05_Volatile_Synchronized implements Runnable {

    private /*volatile*/ int count = 100;

    public static void main(String[] args) {
        T05_Volatile_Synchronized t = new T05_Volatile_Synchronized();
        for (int i = 0; i < 100; i++) {
            new Thread(t, "THREAD" + i).start();
        }
    }

    @Override
    @SneakyThrows
    public /*synchronized*/ void run() {
        Thread.sleep((long) (new Random().nextInt(10) * 0.1));
        count--;
        System.out.println(Thread.currentThread().getName() + " count = " + count);
    }
}